<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2022/2/21
 * Time: 16:14
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Giftbag extends Base
{
    /**
     * 首页
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $gift_name = trim(input('gift_name'), ' ');
            $where['gift_status'] = ['<>', 3];
            // 礼包名称
            if(isset($gift_name) && $gift_name != ''){
                $where['gift_name'] = ['like', '%' . $gift_name . '%'];
            }
        
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_bo_giftbag')->where($where)->count();// 获取总条数
            $lists = Db::table('up_bo_giftbag')
                ->field('gift_id, gift_name, gift_price, gift_indate, gift_status, gift_is_recommend, 
                gift_sale_number, gift_type, gift_time, gift_thumb, gift_fy, gift_first_dec, gift_send_explain, 
                gift_is_zy')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('gift_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加礼包
     * @return mixed|\think\response\Json
     */
    public function giftAdd(){
        if(Request::instance()->isPost()){
            $gift_name = trim(input('gift_name'));
            $gift_thumb = input('gift_thumb');
            $gift_images = input('gift_images');
            $gift_status = input('gift_status');
//            $gift_type = input('gift_type');
            $gift_price = input('gift_price');
            $gift_first_dec = input('gift_first_dec');
            $gift_indate = input('gift_indate');
            $gift_content = input('gift_content');
            $gift_fy = input('gift_fy');
            $gift_send_explain = input('gift_send_explain');
            $gift_is_zy = input('gift_is_zy');
            
            if(!$gift_thumb) return json(['code' => 100, 'msg' => '请上传缩略图']);
            if(!$gift_images) return json(['code' => 100, 'msg' => '请上传图集']);
            if($gift_first_dec > $gift_price) return json(['code' => 100, 'msg' => '减免价格不可超过礼包价格']);
            
            $insert = [
                'gift_name' => $gift_name,
                'gift_thumb' => $gift_thumb,
                'gift_price' => $gift_price,
                'gift_first_dec' => $gift_first_dec,
                'gift_images' => $gift_images,
                'gift_content' => $gift_content,
                'gift_indate' => $gift_indate,
                'gift_status' => $gift_status,
                'gift_fy' => $gift_fy,
                'gift_send_explain' => $gift_send_explain,
//                'gift_type' => $gift_type,
                'gift_time' => date('Y-m-d H:i:s'),
                'gift_is_zy' => $gift_is_zy
            ];
            $res = Db::table('up_bo_giftbag')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
            
        }
        return $this->fetch();
    }
    
    /**
     * 礼包详情
     * @return mixed
     */
    public function giftDetail(){
        $gift_id = input('gift_id');
        $gift = Db::table('up_bo_giftbag')
            ->where('gift_id', $gift_id)
            ->field('gift_name, gift_thumb, gift_price, gift_images, gift_content, gift_indate, gift_status, 
            gift_is_recommend, gift_sale_number, gift_type, gift_time, gift_fy, gift_first_dec, 
            gift_send_explain, gift_is_zy')
            ->find();
        if($gift['gift_images']){
            $gift['gift_images'] = explode(',', $gift['gift_images']);
        }else{
            $gift['gift_images'] = [];
        }
        return $this->fetch('', ['gift' => $gift]);
    }
    
    /**
     * 编辑礼包
     * @return mixed|\think\response\Json
     */
    public function giftEdit(){
        $gift_id = input('gift_id');
        if(Request::instance()->isPost()){
            $gift_name = trim(input('gift_name'));
            $gift_thumb = input('gift_thumb');
            $gift_images = input('gift_images');
            $gift_status = input('gift_status');
            $gift_price = input('gift_price');
            $gift_first_dec = input('gift_first_dec');
            $gift_indate = input('gift_indate');
            $gift_content = input('gift_content');
            $gift_fy = input('gift_fy');
            $gift_send_explain = input('gift_send_explain');
            $gift_is_zy = input('gift_is_zy');
            
            if(!$gift_images) return json(['code' => 100, 'msg' => '请上传图集']);
            if($gift_first_dec > $gift_price) return json(['code' => 100, 'msg' => '减免价格不可超过礼包价格']);
            
            $update = [
                'gift_id' => $gift_id,
                'gift_name' => $gift_name,
                'gift_thumb' => $gift_thumb,
                'gift_images' => $gift_images,
                'gift_status' => $gift_status,
                'gift_price' => $gift_price,
                'gift_first_dec' => $gift_first_dec,
                'gift_indate' => $gift_indate,
                'gift_content' => $gift_content,
                'gift_updatetime' => date('Y-m-d H:i:s'),
                'gift_fy' => $gift_fy,
                'gift_send_explain' => $gift_send_explain,
                'gift_is_zy' => $gift_is_zy
            ];
            $res = Db::table('up_bo_giftbag')->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $gift = Db::table('up_bo_giftbag')
            ->where('gift_id', $gift_id)
            ->field('gift_name, gift_price, gift_thumb, gift_images, gift_content, gift_indate, 
            gift_status, gift_type, gift_fy, gift_first_dec, gift_send_explain, gift_is_zy')
            ->find();
        return $this->fetch('', ['gift' => $gift, 'gift_id' => $gift_id]);
    }
    
    /**
     * 上架
     * @return \think\response\Json
     */
    public function giftOn(){
        $gift_id = input('id');
        $res = Db::table('up_bo_giftbag')->where('gift_id', $gift_id)->setField('gift_status', 1);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 下架
     * @return \think\response\Json
     */
    public function giftDown(){
        $gift_id = input('id');
        $res = Db::table('up_bo_giftbag')->where('gift_id', $gift_id)->setField('gift_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 设置推荐
     * @return \think\response\Json
     */
    public function giftRecommend(){
        $gift_id = input('id');
        $exist = Db::table('up_bo_giftbag')
            ->where(['gift_is_recommend' => 1, 'gift_status' =>['<>', 3]])
            ->find();
        if($exist) return json(['code' => 100, 'msg' => '只能设置一个推荐礼包']);
        $res = Db::table('up_bo_giftbag')->where('gift_id', $gift_id)->setField('gift_is_recommend', 1);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 取消推荐
     * @return \think\response\Json
     */
    public function giftUnrecommend(){
        $gift_id = input('id');
        $res = Db::table('up_bo_giftbag')->where('gift_id', $gift_id)->setField('gift_is_recommend', 2);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 删除
     * @return \think\response\Json
     */
    public function giftDel(){
        $gift_id = input('id');
        $res = Db::table('up_bo_giftbag')->where('gift_id', $gift_id)->setField('gift_status', 3);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '朝族失败']);
        }
    }
}